package com.lin.missyou.repository;

import com.lin.missyou.models.Sku;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SkuRepository extends JpaRepository<Sku,Long> {
    List<Sku> findAllByIdIn(List<Long> ids);

    @Modifying
    @Query("update Sku s set s.stock = s.stock - :quantity where s.id=:sid and s.stock>=:quantity")
    int reduceStock(Long sid, Long quantity);

    @Modifying
    @Query("update Sku s set s.stock = s.stock + :count where s.id=:sid")
    int recoverStock(Long sid, Long count);
}
